//
//  TF_BackButtonView.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2021/11/26.
//  Copyright © 2021 Talkfun. All rights reserved.
//

import UIKit

enum TF_BackChatArea {
case Ask
case Chat
case All
case None
}
class TF_BackButtonView: UIView {

    @objc public let chatBtn:UIButton = UIButton()
    @objc public let askBtn:UIButton = UIButton()
    @objc public let chapter:UIButton = UIButton()
    @objc public let albumBtn:UIButton = UIButton()
    var styleArea = TF_BackChatArea.All
    var lineX:CGFloat = 0;
    var line:UIView = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.chatBtn = UIButton.init()
        self.chatBtn.setTitle("互动区", for: .normal)
        self.chatBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
        self.chatBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        addSubview(self.chatBtn)
//        self.askBtn = UIButton.init()
        self.askBtn.setTitle("提问", for: .normal)
        self.askBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
        self.askBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        addSubview(self.askBtn)
//        self.chapter = UIButton.init()
        self.chapter.setTitle("章节", for: .normal)
        self.chapter.setTitleColor(UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1), for: .normal)
        self.chapter.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        addSubview(self.chapter)
        
//        self.albumBtn = UIButton.init()
        self.albumBtn.setTitle("专辑", for: .normal)
        self.albumBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
        self.albumBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        self.albumBtn.isHidden = true
        addSubview(self.albumBtn)
        
        self.line = UIView.init()
        self.line.backgroundColor = UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1)
    
        addSubview(self.line)
        
    }

    override func layoutSubviews() {
        super.layoutSubviews()
       
        if self.albumBtn.isHidden == false{
            if self.styleArea == .None {
                self.lineX = self.frame.size.width/2.0;
                self.chatBtn.isUserInteractionEnabled = false
                self.askBtn.isUserInteractionEnabled = false
                self.chatBtn.isHidden = true
                self.askBtn.isHidden = true
            }  else if self.styleArea == .All {
                self.lineX = self.frame.size.width/4.0;
                self.chatBtn.isUserInteractionEnabled = true
                self.askBtn.isUserInteractionEnabled = true
                self.chatBtn.isHidden = false
                self.askBtn.isHidden = false
            }  else if self.styleArea == .Ask {
                self.lineX = self.frame.size.width/3.0;
                self.chatBtn.isUserInteractionEnabled = false
                self.askBtn.isUserInteractionEnabled = true
                self.chatBtn.isHidden = true
                self.askBtn.isHidden = false
            } else if self.styleArea == .Chat {
                self.lineX = self.frame.size.width/3.0;
                self.chatBtn.isUserInteractionEnabled = true
                self.askBtn.isUserInteractionEnabled = false
                self.chatBtn.isHidden = false
                self.askBtn.isHidden = true
            }
           
            self.albumBtn.isUserInteractionEnabled = true
        } else {
            if self.styleArea == .None {
                self.lineX = self.frame.size.width;
                self.chatBtn.isUserInteractionEnabled = false
                self.askBtn.isUserInteractionEnabled = false
                self.chatBtn.isHidden = true
                self.askBtn.isHidden = true
            }  else if self.styleArea == .All {
                self.lineX = self.frame.size.width/3.0;
                self.chatBtn.isUserInteractionEnabled = true
                self.askBtn.isUserInteractionEnabled = true
                self.chatBtn.isHidden = false
                self.askBtn.isHidden = false
            }  else if self.styleArea == .Ask {
                self.lineX = self.frame.size.width/2.0;
                self.chatBtn.isUserInteractionEnabled = false
                self.askBtn.isUserInteractionEnabled = true
                self.chatBtn.isHidden = true
                self.askBtn.isHidden = false
            } else if self.styleArea == .Chat {
                self.lineX = self.frame.size.width/2.0;
                self.chatBtn.isUserInteractionEnabled = true
                self.askBtn.isUserInteractionEnabled = false
                self.chatBtn.isHidden = false
                self.askBtn.isHidden = true
            }
            self.albumBtn.isUserInteractionEnabled = false
        }
        
        if self.chatBtn.isHidden {
            self.chatBtn.frame = CGRect(x:0,y:0,width:0,height:40)
        } else {
            self.chatBtn.frame = CGRect(x:0,y:0,width:self.lineX,height:40)
        }
       
      
        if self.askBtn.isHidden {
            self.askBtn.frame = self.chatBtn.frame;
        } else {
            self.askBtn.frame = CGRect(x: self.chatBtn.frame.maxX,y:0,width:self.lineX,height:40)
        }
        
       
        self.chapter.frame = CGRect(x: self.askBtn.frame.maxX,y:0,width:self.lineX,height:40)
        self.albumBtn.frame = CGRect(x: self.chapter.frame.maxX,y:0,width:self.lineX,height:40)
        self.line.frame = CGRect(x:self.chapter.frame.minX + (self.lineX-20)/2.0,y:36,width:20,height:4)
       
      
    }
    
    
    
    @objc public func buttonsAddTarget(target:NSObject, action:Selector) {
        
        self.chatBtn .addTarget(target, action: action, for: .touchUpInside)
        self.askBtn .addTarget(target, action: action, for: .touchUpInside)
        self.chapter .addTarget(target, action: action, for: .touchUpInside)
        self.albumBtn.addTarget(target, action: action, for: .touchUpInside)
      }
    
    @objc public func updateFrame(button:UIButton) {
        if button == self.chatBtn {
            self.line.frame = CGRect(x:self.chatBtn.frame.minX + (self.lineX-20)/2.0,y:36,width:20,height:4)
            self.chatBtn.setTitleColor(UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1), for: .normal)
            self.askBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
            self.chapter.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
            self.albumBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
        } else if button == self.chapter {
            
            self.line.frame = CGRect(x:self.chapter.frame.minX + (self.lineX-20)/2.0,y:36,width:20,height:4)
            self.chapter.setTitleColor(UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1), for: .normal)
            self.askBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
            self.chatBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
            self.albumBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
        } else if button == self.askBtn {
            
            self.line.frame = CGRect(x:self.askBtn.frame.minX + (self.lineX-20)/2.0,y:36,width:20,height:4)
            self.askBtn.setTitleColor(UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1), for: .normal)
            self.chatBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
            self.chapter.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
            self.albumBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
        } else if button == self.albumBtn {
            self.line.frame = CGRect(x:self.albumBtn.frame.minX + (self.lineX-20)/2.0,y:36,width:20,height:4)
            self.albumBtn.setTitleColor(UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1), for: .normal)
            self.chatBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
            self.chapter.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
            self.askBtn.setTitleColor(UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1), for: .normal)
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
